package cn.mcsys.worker.core.conf;

import java.awt.geom.Point2D;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

public class LoadSpecialLineConfiguration {

    public static final String SHIQU = "shiqu";
    public static final String ZHUANXIAN = "zhuanxian";
    public static final String DOUMEN = "doumen";

    public static Map<String,List<Point2D.Double>> lineMap = new HashMap<>();

    static {
        InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("line-point-lon-lat.properties");

        Properties properties = new Properties();

        try {
            properties.load(in);
            Map<String, List<Point2D.Double>> map = createridePoint2D(properties);
//        市区电子围栏
            List<Point2D.Double> shiqu = map.get(SHIQU);
//        航展专线电子围栏
            List<Point2D.Double> zhuanxian = map.get(ZHUANXIAN);
//        斗门电子围栏
            List<Point2D.Double> doumen = map.get(DOUMEN);

            lineMap.put(SHIQU,shiqu);
            lineMap.put(ZHUANXIAN,zhuanxian);
            lineMap.put(DOUMEN,doumen);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static Map<String,List<Point2D.Double>> createridePoint2D(Properties properties){
        HashMap<String, List<Point2D.Double>> map = new HashMap<>();
        List<Point2D.Double> shiqu = new ArrayList<>();
        List<Point2D.Double> zhuanxian = new ArrayList<>();
        List<Point2D.Double> doumen = new ArrayList<>();
        for (int i = 1; i <= 26; i++) {
            if(i <= 22){
                Double[] shiquLonLat = parseLonLat(properties.getProperty("shiqu-"+i));
                shiqu.add(new Point2D.Double(shiquLonLat[0],shiquLonLat[1]));
            }
            if(i <= 11){
                Double[] doumenLonLat = parseLonLat(properties.getProperty("doumen-"+i));
                doumen.add(new Point2D.Double(doumenLonLat[0],doumenLonLat[1]));
            }
            Double[] zhuanxianLonLat = parseLonLat(properties.getProperty("zhuanxianlu-"+i));
            zhuanxian.add(new Point2D.Double(zhuanxianLonLat[0],zhuanxianLonLat[1]));

        }
        map.put(SHIQU,shiqu);
        map.put(ZHUANXIAN,zhuanxian);
        map.put(DOUMEN,doumen);
        return map;
    }

    public static Double[] parseLonLat(String lonlat){
        String[] lls = lonlat.split(",");
        Double[] lld = new Double[2];
        for (int i = 0; i < lls.length; i++) {
            lld[i] = Double.valueOf(lls[i]);
        }
        return lld;
    }

}
